		TITLE	ICODE - Copyright (c) SLR Systems 1994

		INCLUDE	MACROS
if	fg_pe
		INCLUDE	SEGMSYMS
		INCLUDE	PE_STRUC

		PUBLIC	CREATE_ICODE_DATA


		.DATA

		EXTERNDEF	FIX2_LD_TYPE:BYTE,TEMP_RECORD:BYTE

		EXTERNDEF	FIX2_LD_LENGTH:WORD

		EXTERNDEF	PE_THUNKS_RVA:DWORD,ICODE_SM_START:DWORD,FIX2_LDATA_PTR:DWORD,FIX2_LDATA_LOC:DWORD
		EXTERNDEF	FIX2_SM_START:DWORD,PE_BASE:DWORD,FIRST_IMPMOD_GINDEX:DWORD

		EXTERNDEF	IMPMOD_GARRAY:STD_PTR_S


		.CODE	PASS2_TEXT

		EXTERNDEF	DO_PE_RELOC:PROC,EXE_OUT_LDATA:PROC


ICODE_VARS		STRUC

MY_RELOC_ADDR_BP	DD	?
MY_THUNKS_ADDR_BP	DD	?
BYTES_SO_FAR_BP		DD	?
TABLE_PTR_BP		DD	?
TABLE_CNT_BP		DD	?

ICODE_VARS		ENDS


FIX	MACRO	X

X	EQU	([EBP-SIZE ICODE_VARS].(X&_BP))

	ENDM


FIX	MY_RELOC_ADDR
FIX	MY_THUNKS_ADDR
FIX	BYTES_SO_FAR
FIX	TABLE_PTR
FIX	TABLE_CNT


CREATE_ICODE_DATA	PROC
		;
		;BUILD STRUCTURES IN TEMP_RECORD THAT HANDLE INDIRECT JUMPS TO IMPORTED DATA...
		;
		PUSHM	EBP,EDI,ESI,EBX

		MOV	EBP,ESP
		ASSUME	EBP:PTR ICODE_VARS
		SUB	ESP,SIZE ICODE_VARS

		MOV	EAX,ICODE_SM_START
		MOV	ESI,OFF TEMP_RECORD

		ADD	EAX,2
		MOV	FIX2_LDATA_PTR,ESI

		MOV	MY_RELOC_ADDR,EAX	;POINT TO ADDRESS
		MOV	EAX,PE_THUNKS_RVA

		ADD	EAX,PE_BASE
		MOV	TABLE_PTR,ESI

		MOV	MY_THUNKS_ADDR,EAX
		XOR	EAX,EAX

		MOV	FIX2_LD_TYPE,MASK BIT_LE

		MOV	TABLE_CNT,MAX_RECORD_LEN/6

		MOV	BYTES_SO_FAR,EAX
		MOV	ESI,FIRST_IMPMOD_GINDEX

L1$:
		CONVERT	ESI,ESI,IMPMOD_GARRAY
		ASSUME	ESI:PTR IMPMOD_STRUCT

		CALL	HANDLE_IMPMOD		;ESI IS IMPMOD

		MOV	ESI,[ESI]._IMPM_NEXT_GINDEX

		TEST	ESI,ESI
		JNZ	L1$

		CALL	FLUSH_TABLE

		MOV	ESP,EBP
		XOR	EAX,EAX

		MOV	FIX2_LDATA_PTR,EAX

		POPM	EBX,ESI,EDI,EBP

		RET

CREATE_ICODE_DATA	ENDP


HANDLE_IMPMOD	PROC	NEAR	PRIVATE
		;
		;OUTPUT ANY VECTORS FROM THIS MODULE
		;
		MOV	EBX,[ESI]._IMPM_N_IMPORTS

		TEST	EBX,EBX
		JZ	L9$

L1$:
		CALL	STORE_JUMP

		DEC	EBX
		JNZ	L1$

		ADD	MY_THUNKS_ADDR,4	;SKIP NULL AT END OF MODULE
L9$:
		RET

HANDLE_IMPMOD	ENDP


STORE_JUMP	PROC	NEAR
		;
		;STORE A RELOCATION ENTRY
		;
		MOV	EDX,MY_RELOC_ADDR

		MOV	EAX,EDX
		ADD	EDX,6

		MOV	MY_RELOC_ADDR,EDX
		CALL	DO_PE_RELOC
		;
		;STORE AN INDIRECT JUMP INSTRUCTION
		;
		MOV	EDX,TABLE_PTR
		MOV	EAX,25FFH

		MOV	[EDX],AX
		MOV	ECX,MY_THUNKS_ADDR

		MOV	EAX,TABLE_CNT
		MOV	[EDX+2],ECX

		ADD	ECX,4
		ADD	EDX,6

		MOV	MY_THUNKS_ADDR,ECX
		DEC	EAX

		MOV	TABLE_PTR,EDX
		MOV	TABLE_CNT,EAX

		JZ	FLUSH_TABLE

		RET

STORE_JUMP	ENDP


FLUSH_TABLE	PROC	NEAR	PRIVATE
		;
		;
		;
		MOV	ECX,TABLE_PTR
		MOV	EAX,OFF TEMP_RECORD

		SUB	ECX,EAX
		JZ	L9$

		MOV	FIX2_LD_LENGTH,CX
		MOV	TABLE_PTR,EAX

		MOV	EAX,BYTES_SO_FAR
		MOV	EDX,FIX2_SM_START

		ADD	ECX,EAX
		ADD	EAX,EDX

		MOV	BYTES_SO_FAR,ECX
		MOV	FIX2_LDATA_LOC,EAX

		MOV	TABLE_CNT,MAX_RECORD_LEN/6

		JMP	EXE_OUT_LDATA

L9$:
		RET

FLUSH_TABLE	ENDP

endif

		END

